﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace BenhVien
{
    public partial class ChinhSuaVatTu : Form
    {
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        Connect conn = new Connect();
        public ChinhSuaVatTu()
        {
            InitializeComponent();
        }

        private void bt_Xoa_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bt_them_Click(object sender, EventArgs e)
        {
            DataRow row = dt.Select("MaVT like '" + txtMaVT.Text + "'")[0];
            row.BeginEdit();
            row["Ten"] = txtTenVT.Text;
            row["MoTa"] = txtMT.Text;
            row["SoLuongTrongKho"] = txtSL.Text;
            row["GiaTien"] = txtGT.Text;
            row.EndEdit();
            SqlCommand commandUpdate = new SqlCommand();
            commandUpdate.Connection = conn.con;
            commandUpdate.CommandType = CommandType.Text;
            commandUpdate.CommandText = @"Update KhoVatTu Set Ten= @Ten,
                                                              MoTa= @MoTa,
                                                              SoLuongTrongKho= @SoLuongTrongKho,
                                                              GiaTien=@GiaTien
                                                              where MaVT=@MaVT";
            commandUpdate.Parameters.Add("@MaVT", SqlDbType.NVarChar, 50, "MaVT");
                    commandUpdate.Parameters.Add("@Ten", SqlDbType.NVarChar, 50, "Ten");
                    commandUpdate.Parameters.Add("@MoTa", SqlDbType.NVarChar, 50, "MoTa");
                    commandUpdate.Parameters.Add("@SoLuongTrongKho", SqlDbType.Int, 50, "SoLuongTrongKho");
                    commandUpdate.Parameters.Add("@GiaTien", SqlDbType.Money, 50, "GiaTien");
            da.UpdateCommand = commandUpdate;
            da.Update(dt);
            MessageBox.Show("ban da chinh sua thanh cong", "chuc mung", MessageBoxButtons.OK);
        
        }
        public void binding()
        {
            txtMaVT.DataBindings.Clear();
            txtMaVT.DataBindings.Add("Text", dataGridView1.DataSource, "MaVT");
            txtTenVT.DataBindings.Clear();
            txtTenVT.DataBindings.Add("Text", dataGridView1.DataSource, "Ten");
            txtMT.DataBindings.Clear();
            txtMT.DataBindings.Add("Text", dataGridView1.DataSource, "MoTa");
            txtSL.DataBindings.Clear();
            txtSL.DataBindings.Add("Text", dataGridView1.DataSource, "SoLuongTrongKho");
            txtGT.DataBindings.Clear();
            txtGT.DataBindings.Add("Text", dataGridView1.DataSource, "GiaTien");
        }
        public void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = conn.con;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select * from KhoVatTu";
            da.SelectCommand = command;
            da.Fill(dt);
            dataGridView1.DataSource = dt;

        }

        private void ChinhSuaVatTu_Load(object sender, EventArgs e)
        {
            conn.connect();
            getdata();
            binding();
        }

    }
}
